package cn.tedu.straw.portal.Schedule;

import cn.tedu.straw.portal.mapper.QuestionMapper;
import cn.tedu.straw.portal.service.IQuestionService;
import cn.tedu.straw.portal.service.ITagService;
import cn.tedu.straw.portal.service.IUserQuestionService;
import cn.tedu.straw.portal.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * @author shkstart
 * @create 2020-07-21 14:38
 */
@Slf4j
@Component
@EnableScheduling
public class CacheSchedule {
    @Autowired
    private ITagService tagService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IQuestionService questionService;
    /**
     * 缓存锁,凡是写入(添加,移除)缓存的数据时使用这个锁
     * public : 多个类都需要使用到这把锁
     * static : 具有唯一性,能保证互斥
     * final  : 不允许任何位置修改或重新创建对象
     * Object : 不关心锁的类型,
     */
    public  static  final Object lock_CANCHE= new Object();

    @Scheduled(initialDelay =10*60*1000,fixedRate =10*60*1000 )
    public void clearCache(){
        synchronized (lock_CANCHE) {
            tagService.getCachedTags().clear();
            tagService.getCachedTagsMap().clear();
            log.debug("clerar tags cache...");
            userService.getCachedTeachers().clear();
            log.debug("clerar Teachers cache...");
        }
    }
@Scheduled(initialDelay = 10*1000,fixedRate = 10*1000)
public void clearMostHits(){
    synchronized (lock_CANCHE) {
        questionService.getCachedMostHits().clear();
        log.debug("clear MostHits cache...");
    }
}
}
